/*
* Copyright (c)  2017, ueboot.com
* All rights reserved.
* 2017-11-02 17:23:48
*/
package com.ueboot.weishop.admin.controller.finance;

import com.ueboot.core.http.response.Response;
import com.ueboot.weishop.admin.controller.finance.vo.*;
import com.ueboot.weishop.entity.finance.SettlementBatch;
import com.ueboot.weishop.service.finance.SettlementBatchService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;


/**
 *  Created by yangkui  on 2017-11-02 17:23:48.
 * @author yangkui
 * @since 2.0 by autocode
 */
@Slf4j
@RestController
@RequestMapping(value = "/settlementBatch")
public class SettlementBatchController {

    @Autowired
    private SettlementBatchService settlementBatchService;


    @RequiresPermissions("settlementBatch:read")
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public Response<Page<SettlementBatchResp>> page(@PageableDefault(value = 15, sort = { "id" }, direction = Sort.Direction.DESC)
                                                     Pageable pageable, @RequestBody(required = false) SettlementBatchFindReq req){
        Page<SettlementBatch> entities = settlementBatchService.findBy(pageable);
        Page<SettlementBatchResp> body = entities.map(entity -> {
            SettlementBatchResp resp = new SettlementBatchResp();
            BeanUtils.copyProperties(entity, resp);
            return resp;
        });

        return new Response<>(body);
    }


    @RequiresPermissions("settlementBatch:save")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Response<Void> save(@RequestBody SettlementBatchReq req) {
        SettlementBatch entity = null;
        if (req.getId() == null) {
            entity = new SettlementBatch();
        } else {
            entity = settlementBatchService.get(req.getId());
        }
        BeanUtils.copyProperties(req, entity);
        settlementBatchService.save(entity);
        return new Response<>();
    }

    @RequiresPermissions("settlementBatch:delete")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Response<Void> delete(Long[] id) {
        settlementBatchService.delete(id);
        return new Response<>();
    }

    @RequiresPermissions("settlementBatch:read")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Response<SettlementBatchResp> get(@PathVariable Long id) {
        SettlementBatch entity = settlementBatchService.get(id);
        SettlementBatchResp resp = new SettlementBatchResp();
        BeanUtils.copyProperties(entity, resp);
        return new Response<>(resp);
    }

}
